/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;
using System.Collections.Generic;
using Xceed.Silverlight.Data.Stats;
using System.Linq;


namespace Xceed.Silverlight.Data
{
  internal struct StatParameters
  {
    private static readonly StatFunctionComparer StatFunctionComparerInstance = new StatFunctionComparer();

    #region Constructors

    internal StatParameters( IEnumerable<StatFunction> statFunctions )
      : this( statFunctions, DataPath.Empty )
    {
    }

    internal StatParameters( IEnumerable<StatFunction> statFunctions, DataPath targetPath )
      : this()
    {
      if( statFunctions == null )
        throw new ArgumentNullException( "statFunctions" );

      if( targetPath == null )
        throw new ArgumentNullException( "targetPath" );

      this.StatFunctions = statFunctions;
      this.TargetPath = targetPath;
    }

    #endregion

    #region StatFunction Property

    internal IEnumerable<StatFunction> StatFunctions
    {
      get;
      private set;
    }

    #endregion

    #region GroupValues Property

    internal IEnumerable<object> GroupValues
    {
      get
      {
        return this.TargetPath.Path
          .Where( ( dataItem ) => dataItem is GroupDataItem )
          .Select( ( groupItem ) => groupItem.Data );
      }
    }

    #endregion

    #region TargetPath

    internal DataPath TargetPath
    {
      get;
      private set;
    }

    #endregion

    public override bool Equals( object obj )
    {
      if( !( obj is StatParameters ) )
        return false;

      StatParameters otherParameters = ( StatParameters )obj;

      return object.Equals( this.TargetPath, otherParameters.TargetPath )
        && this.StatFunctions.SequenceEqual( otherParameters.StatFunctions, StatParameters.StatFunctionComparerInstance );
    }

    public override int GetHashCode()
    {
      if( m_hashCode == null )
      {
        m_hashCode = this.TargetPath.GetHashCode();

        foreach( StatFunction function in this.StatFunctions )
        {
          m_hashCode ^= function.GetEquivalenceKey();
        }
      }

      return m_hashCode.Value;
    }

    public static bool operator ==( StatParameters p1, StatParameters p2 )
    {
      return object.Equals( p1, p2 );
    }

    public static bool operator !=( StatParameters p1, StatParameters p2 )
    {
      return !( p1 == p2 );
    }

    private Nullable<int> m_hashCode; 

    private class StatFunctionComparer : IEqualityComparer<StatFunction>
    {

      #region IEqualityComparer<StatFunction> Members

      public bool Equals( StatFunction x, StatFunction y )
      {
        return StatFunction.AreEquivalents( x, y );
      }

      public int GetHashCode( StatFunction obj )
      {
        return obj.GetEquivalenceKey();
      }

      #endregion
    }
  }
}
